package com.jzo2o.orders.manager.service.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.foundations.ServeApi;
import com.jzo2o.api.foundations.dto.response.ServeAggregationResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 自定义Feign客户端用于熔断降级
 * @author JIAN
 */
@Slf4j
@Component
@SuppressWarnings("unused")
public class FoundationClient {
    @Resource
    private ServeApi serveApi;

    @SentinelResource(value = "serveById",
            fallback = "getServeByIdFallback", blockHandler = "getServeByIdBlockHandler")
    public ServeAggregationResDTO getServeById(Long id) {
        return serveApi.findById(id);
    }

    public ServeAggregationResDTO getServeByIdFallback(Long id, Throwable throwable) {
        log.warn("服务信息接口异常(未触发熔断), 服务id: {}", id, throwable);
        return null;
    }

    public ServeAggregationResDTO getServeByIdBlockHandler(Long id, BlockException blockException) {
        log.warn("服务信息接口异常(触发熔断降级), 服务id: {}", id, blockException);
        return null;
    }
}